package com.pzhyd.sc.biz.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pzhyd.sc.biz.inter.IvotedInfoBiz;
import com.pzhyd.sc.dao.inter.IBaseDao;
import com.pzhyd.sc.entity.Votedinfo;
import com.pzhyd.sc.entity.Voteoption;
import com.pzhyd.sc.util.DateUtil;
import com.pzhyd.sc.util.StringUtil;

public class VotedInfoBizImpl implements IvotedInfoBiz {
	private static final Log LOGGER = LogFactory.getLog(VotedInfoBizImpl.class); // 日志对象
	private IBaseDao idao;
	
	public Votedinfo getVotedInfo(Integer id) {
		Votedinfo  votedinfo=null;
		try {
			votedinfo=(Votedinfo)this.idao.searchById(Votedinfo.class, id);
		} catch (Exception e) {
			LOGGER.error("查询评选人信息对象失败，原因是"+e.getMessage(),e);
		}
		return votedinfo;
	}

	 
	public List getVotedInfoList(Map<String, String> param) {
		List<Votedinfo> list=null;
		try {
			 StringBuffer hql=new StringBuffer("From Votedinfo v where 1=1 order by v.viClicks desc");
			 list=this.idao.searchHql(hql.toString(),new ArrayList());
			 if(list!=null && list.size()>0){
				 List dataList=new ArrayList();
				 for(Votedinfo votedinfo:list){
					 String viIntro=votedinfo.getViIntro();
					 if(viIntro!=null){
						 if(viIntro.length()>22){
							 viIntro=viIntro.substring(0, 21);
							 viIntro+="...";
							 votedinfo.setViIntro(viIntro);
							 dataList.add(votedinfo);
						 }else{
							 dataList.add(votedinfo);
						 }
					 }else{
						 dataList.add(votedinfo);
					 }
				 }
				 list=dataList;
			 }
		} catch (Exception e) {
			LOGGER.error("查询评选人信息列表失败，原因是"+e.getMessage(),e);
		}
		return list;
	}

	 
	public Integer updateVotedInfo(Votedinfo votedInfo) {
		Integer retValue=null;
		try {
			Votedinfo votedinfoBean=(Votedinfo)this.idao.searchById(Votedinfo.class, votedInfo.getViId());
			if(!StringUtil.isEmpty(votedInfo.getViClicks())){
				votedinfoBean.setViClicks(votedinfoBean.getViClicks()+votedInfo.getViClicks());
			}
			if(!StringUtil.isEmpty(votedInfo.getViTimes())){
				votedinfoBean.setViTimes(votedinfoBean.getViTimes()+votedInfo.getViTimes());
			}
			retValue=this.idao.update(votedinfoBean);
		} catch (Exception e) {
			retValue=0;
			LOGGER.error("修改评选人信息对象失败，原因是"+e.getMessage(),e);
		}
		return retValue;
	}

	public Integer updateVotedInfo(String voAccount, String viId,String voIpaddr) {
		try {
			String voTime=DateUtil.toFormatString(new Date(), "yyyyMMdd");
			List dataList=getVotedOptionList(voAccount,voTime); 
			if(dataList==null || dataList.size()==0){
				//新增投票记录
				Voteoption voteoption=new Voteoption();
				voteoption.setVoAccount(voAccount);
				voteoption.setVoIpaddr(voIpaddr);
				voteoption.setVoTime(voTime);
				voteoption.setVotedinfo((Votedinfo)this.idao.searchById(Votedinfo.class,Integer.valueOf(viId)));
				this.idao.insert(voteoption);
				//修改评选人信息被投次数
				Votedinfo votedinfo=(Votedinfo)this.idao.searchById(Votedinfo.class,Integer.valueOf(viId));
				votedinfo.setViTimes(votedinfo.getViTimes()+1);
				this.idao.update(votedinfo);
				return 1;
			}else{
				return -1;
			}
		} catch (Exception e) {
			LOGGER.error("修改评选人信息失败，原因是"+e.getMessage(),e);
			return 0;
		}
	}
	/*************************************************
	 *************** 辅助方法*************************
	 *************************************************/
	private  List getVotedOptionList(String voAccount, String voTime) {
		 List rtnList=null;
		 try {
			String hql="from Voteoption v where 1=1 and v.voAccount=? and v.voTime=?";
			 List paramList=new ArrayList();
			 paramList.add(voAccount);
			 paramList.add(voTime);
			 rtnList=this.idao.searchHql(hql, paramList);
		} catch (Exception e) {
			LOGGER.error("查询投票记录信息列表失败，原因是"+e.getMessage(),e);
		}
		return rtnList;
	}
	/**
	 * 以下是get,set方法
	 */
	public IBaseDao getIdao() {
		return idao;
	}

	public void setIdao(IBaseDao idao) {
		this.idao = idao;
	}
}
